<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="模型供应商" name="provider">
						<a-input v-model:value="searchFormState.provider" placeholder="请输入模型供应商" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('aiEmbeddingConfigAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-button
						v-if="hasPerm('aiEmbeddingConfigBatchDelete')"
						buttonName="批量删除"
                        icon="DeleteOutlined"
						:selectedRowKeys="selectedRowKeys"
						@batchCallBack="deleteBatchAiEmbeddingConfig"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('aiEmbeddingConfigEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['aiEmbeddingConfigEdit', 'aiEmbeddingConfigDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteAiEmbeddingConfig(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('aiEmbeddingConfigDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
</template>

<script setup name="embeddingconfig">
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import aiEmbeddingConfigApi from '@/api/ai/aiEmbeddingConfigApi'
	const searchFormState = ref({})
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		{
			title: '配置名称',
			dataIndex: 'name'
		},
		{
			title: '模型供应商',
			dataIndex: 'provider'
		},
		{
			title: '模型名称',
			dataIndex: 'modelName'
		},
		{
			title: '模型部署名称',
			dataIndex: 'modelDeployment'
		},
		{
			title: 'API接口地址',
			dataIndex: 'apiUrl'
		},
		{
			title: 'API密钥',
			dataIndex: 'apiKey'
		},
		{
			title: 'API密钥secret',
			dataIndex: 'apiSecret'
		},
		{
			title: '嵌入向量维度',
			dataIndex: 'dimensionSize'
		},
		{
			title: '批处理大小',
			dataIndex: 'batchSize'
		},
		{
			title: '是否截断超长文本(0-否,1-是)',
			dataIndex: 'truncate'
		},
		{
			title: '最大token数',
			dataIndex: 'maxTokens'
		},
		{
			title: '是否移除换行符(0-否,1-是)',
			dataIndex: 'stripNewLines'
		},
		{
			title: '超时时间(秒)',
			dataIndex: 'timeout'
		},
		{
			title: '代理服务器地址',
			dataIndex: 'proxyHost'
		},
		{
			title: '代理服务器端口',
			dataIndex: 'proxyPort'
		},
		{
			title: '状态（1-启用，2-禁用）',
			dataIndex: 'status'
		},
		{
			title: '排序',
			dataIndex: 'sort'
		},
		{
			title: '删除标志（0-未删除，1-已删除）',
			dataIndex: 'delFlag'
		},
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['aiEmbeddingConfigEdit', 'aiEmbeddingConfigDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 150
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = cloneDeep(searchFormState.value)
		return aiEmbeddingConfigApi.aiEmbeddingConfigPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteAiEmbeddingConfig = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		aiEmbeddingConfigApi.aiEmbeddingConfigDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchAiEmbeddingConfig = (params) => {
		aiEmbeddingConfigApi.aiEmbeddingConfigDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
</script>
